/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Misc tests.
 *//*
 * LEGAL:   COPYRIGHT (C) 2010 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define TESTING 1
#include "test.hh"
#include "base/auto_flag.hh"
#include "base/busy.hh"

/*****************************************************************************
 * Test AutoFlag class.
 *****************************************************************************/
bool
TestAutoFlag( void )
{
    bool flag = false;
    {
        AutoFlag autoflag( &flag );     // ctor should assign true
        if ( not flag ) return Fail();
    }
    if ( flag ) return Fail();          // dtor should assign false

    return true;
}

/*****************************************************************************
 * Test BUSY_BEGIN/BUSY_END.
 *****************************************************************************/
int sBusyCalls = 0;

int
BusyFunc( void )
{

// Catch if busy flag stays true aberrantly.

BUSY_BEGIN {

    return ++sBusyCalls;

} BUSY_END

    return -1;  // shutup compiler
}

bool
TestBusy( void )
{
    if ( BusyFunc() != 1 ) return Fail();
    if ( BusyFunc() != 2 ) return Fail();
    if ( BusyFunc() != 3 ) return Fail();

    return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int main( int argc, char** argv )
{
    RUN_TEST( "misc", TEST_ERROR_MODE_ABORT,
              TestAutoFlag()
          and TestBusy() )
}
